package com.example.dzcfinal.SQLite;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;


import java.util.ArrayList;
import java.util.List;

public class SQLite extends SQLiteOpenHelper {


    // 数据库版本号
    public SQLite(@Nullable Context context){
        super(context, "lz.db", null, 2);

    }

    //    创建数据库和初始化数据
    @Override
    public void onCreate(SQLiteDatabase db) {
//        创建goods表以初始化列（goodname，type，writer）
        db.execSQL("CREATE TABLE goods(g_id INTEGER PRIMARY KEY AUTOINCREMENT ,goodname VARCHAR(40) ,type VARCHAR(15) ,writer VARCHAR(10))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
//看表中有没有这个商品
    public int findGood(String nameText){
//        数据库阅读对象
        SQLiteDatabase db = getReadableDatabase();
//        sql语句（查询）
        Cursor cursor = db.query("goods",null,"goodname=?",new String[]{nameText},null,null,null);
        if (cursor.getCount()!=0){
            while (cursor.moveToNext()){
                @SuppressLint("Range") Integer gid = cursor.getInt(cursor.getColumnIndex("g_id"));
                @SuppressLint("Range") String goodname = cursor.getString(cursor.getColumnIndex("goodname"));
                @SuppressLint("Range") String type = cursor.getString(cursor.getColumnIndex("type"));
                @SuppressLint("Range") String writer = cursor.getString(cursor.getColumnIndex("writer"));
                return gid;
            }
        }
        cursor.close();
        db.close();
        return -1;
    }

//    遍历数据库中所有商品信息
    @SuppressLint("Range")
    public List<Goods> getAllGoods(){
        String sql = "select * from goods";
        SQLiteDatabase db = getReadableDatabase();
        List<Goods> Goods = new ArrayList<Goods>();
        Goods good = null;
        Cursor cursor = db.rawQuery(sql,null);
        while(cursor.moveToNext()){
            good = new Goods();
            good.setId(cursor.getInt(cursor.getColumnIndex("g_id")));
            good.setGoodname(cursor.getString(cursor.getColumnIndex("goodname")));
            good.setType(cursor.getString(cursor.getColumnIndex("type")));
            good.setWriter(cursor.getString(cursor.getColumnIndex("writer")));

            Goods.add(good);
        }
        cursor.close();
        db.close();
        return Goods;
    }
//删除这个商品
    public void deleteGood(String Text) {
        SQLiteDatabase db = getReadableDatabase();
        db.delete("goods","goodname=?",new String[]{Text});
        db.close();

    }

//插入商品
    public void insertGood(String nameText,String typeText,String writerText){
        SQLiteDatabase db = getReadableDatabase();
        // 创建ContentValues对象
        ContentValues values = new ContentValues();
        // 向该对象中插入键值对
        values.put("goodname",nameText);
        values.put("type",typeText);
        values.put("writer",writerText);
        // 插入数据到数据库当中：insert()
        // c. 插入数据到数据库当中：insert()
        long id = db.insert("goods",null,values);
        Log.i("InsertId", String.valueOf(id));
        // 参数1：要操作的表名称
        // 参数2：SQl不允许一个空列，若ContentValues是空，那么这一列被明确的指明为NULL值
        // 参数3：ContentValues对象
        db.close();
    }
}
